package cn.itcast.aip;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.util.Base64Util;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
/**
 * 2、百度AIP： 人脸识别  抽取的工具类
 */
public class Sample1 {
    //设置APPID/AK/SK
    public static final String APP_ID = "23152909";
    public static final String API_KEY = "RvZZEac6CZugdzuDBerLVbuD";
    public static final String SECRET_KEY = "G40Qu4g3AFyw0iml7kl5NA4CDNqSzCoB";

    public static void main(String[] args) throws IOException {
        String file = "F:\\3.jpg";
        File file1 = new File(file);
        byte[] bytes = Files.readAllBytes(file1.toPath());
        System.out.println(detect(bytes));
        
    }

    public static boolean detect(byte [] body) {

        AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");


        String imageType = "BASE64";

        String image = Base64Util.encode(body);

        // 人脸检测
        JSONObject res = client.detect(image, imageType, options);

        int code = res.getInt("error_code");

        return code == 0;
    }
}